package cn.j229.bug.zhongxuan.hualian.service.impl;

import cn.j229.bug.zhongxuan.hualian.mapper.TicketMapper;
import cn.j229.bug.zhongxuan.hualian.pojo.Ticket;
import cn.j229.bug.zhongxuan.hualian.service.TicketService;
import cn.j229.bug.zhongxuan.hualian.utils.CreateNumber;
import cn.j229.bug.zhongxuan.hualian.utils.IsNullUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author smile_ZhongXuan
 * @Date 2020/10/20 14:32
 * @Version 1.0
 */
@Service("ticketService")
public class TicketServiceImpl implements TicketService {
    @Resource
    TicketMapper ticketMapper = null;

    @Override
    public int addTicketSelective(Ticket ticket) {
        ticket.setNum(CreateNumber.getNum());
        return ticketMapper.insertSelective(ticket);
    }

    @Override
    public Map findAllTicketByPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Ticket> selectAll = ticketMapper.selectAll();
        PageInfo<Ticket> ticketPageInfo = new PageInfo<>(selectAll);
        Map map = new HashMap();
        map.put("key",ticketPageInfo.getList());
        return map;
    }

    @Override
    public String exportTicketList(HSSFWorkbook hssfWorkbook) throws UnsupportedEncodingException {
        // 先获取数据
        List<Ticket> tickets =  ticketMapper.selectAll();

        HSSFSheet sheet = hssfWorkbook.createSheet("sheetName");  // 工作表的名称
        String fileName = "TicketList.xls";   // 文件名
        // 完美实现中文文件名的转换
        fileName = URLEncoder.encode(fileName,"utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "序号", "优惠券编号", "优惠券名称","优惠券描述","优惠券注意事项","优惠券领用有效期","优惠券使用有效期","优惠券封面图片","优惠券状态","优惠券是否可重复领取"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 添加数据行:
        int rowNum = 1;
        for (Ticket item : tickets) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);   // 自增长变量
            dataRow.createCell(1).setCellValue(item.getNum());
            dataRow.createCell(2).setCellValue((String) IsNullUtils.isNull(item.getName()));
            dataRow.createCell(3).setCellValue((String) IsNullUtils.isNull(item.getDescription()));
            dataRow.createCell(4).setCellValue((String) IsNullUtils.isNull(item.getAttentions()));
            dataRow.createCell(5).setCellValue((String) IsNullUtils.isNullDate(item.getCollectDate()));
            dataRow.createCell(6).setCellValue((String) IsNullUtils.isNullDate(item.getUseDate()));
            dataRow.createCell(7).setCellValue((String) IsNullUtils.isNull(item.getImage()));
            dataRow.createCell(8).setCellValue(String.valueOf(IsNullUtils.isNull(item.getState())));
            dataRow.createCell(9).setCellValue(String.valueOf(IsNullUtils.isNull(item.getRepeatable())));
            rowNum++;
        }
        return fileName;
    }

    @Override
    public int updateTicketState(Ticket ticket) {
        return ticketMapper.updateByPrimaryKeySelective(ticket);
    }

}
